#!/usr/bin/env tclsh8.5
# $Id: pkgcreate.tcl 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# version "$Revision: 7557b5d4d82b $"
# author "$Author: pgurumur $"

set progname [lindex [split $argv0 '/'] end]

proc optparse { argv argc } {
   global progname

   if {$argc <= 0} {
      usage 
   } else {
      array set args {}
      set count 0
      foreach element $argv {
         set args($count) $element
         incr count
      }
   
      set src ""
      set dir ""
      set verbose 0

      foreach element [array name args] {
         if {$args($element) == "-s" || $args($element) == "--source"} {
            set src $args([incr element])
         } elseif {$args($element) == "-d" || $args($element) == "--dir"} {
            set dir $args([incr element])
         } elseif {$args($element) == "-v" || $args($element) == "--verbose"} {
            set verbose 1
         } elseif {$args($element) == "-h" || $args($element) == "--help"} {
            usage
         }
      }

      if {[string length $src] <= 0} {
         set src "*.tcl"
      }

      if {[string length $dir] > 0} {
         if {[file exists $dir] && [file isdirectory $dir]} {
            if {$verbose == 1} {
               pkg_mkIndex -verbose $dir $src
            } else {
               pkg_mkIndex $dir $src
            }
         } else {
            puts "$progname: $dir either does not exist or is not a directory"
            exit 1
         }
      } else {
         puts "$progname: no directory given, cannot proceed!"
         exit 1
      }
   }
}

proc usage {} {
   global progname
   puts "$progname options"
   puts "options: "
   puts "\t  -s | --src : source file, default: *.tcl"
   puts "\t  -d | --dir : directory where to create pkg index file"
   puts "\t  -v | --verbose : Whether to make this verbose"
   puts "\t  -h | --help : print this help"
   exit 1
}

optparse $argv $argc
